class OrganizeController {

    static allowedMethods = [save: "POST", update: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100) 
 
        if (params.id && params.id != "") {
            def parent = Organize.get(params.id)
             ['parent' : parent ,
             organizeInstanceList: Organize.findAllByParent(parent ,params),
             organizeInstanceTotal: Organize.countByParent(parent)]
        }else{
            [organizeInstanceList: Organize.findAllByParentIsNull(params), 
            organizeInstanceTotal: Organize.countByParentIsNull()]
        }
    }

    def create = {
        def organizeInstance = new Organize()
        organizeInstance.properties = params
        if (params.id) {
            organizeInstance.parent = Organize.get(params.id)
        }
        return [organizeInstance: organizeInstance]
    }

    def save = {
        def organizeInstance = new Organize(params)
        if (organizeInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'organize.label', default: 'Organize'), organizeInstance])}"
            redirect(action: "list" , id: params.parent.id)
        }
        else {
            render(view: "create", model: [organizeInstance: organizeInstance])
        }
    }

    def show = {
        def organizeInstance = Organize.get(params.id)
        if (!organizeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'organize.label', default: 'Organize'), params.id])}"
            redirect(action: "list")
        }
        else {
            [organizeInstance: organizeInstance]
        }
    }

    def edit = {
        def organizeInstance = Organize.get(params.id)
        if (!organizeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'organize.label', default: 'Organize'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [organizeInstance: organizeInstance]
        }
    }

    def update = {
        def organizeInstance = Organize.get(params.id)
        if (organizeInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (organizeInstance.version > version) {
                    
                    organizeInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'organize.label', default: 'Organize')] as Object[], "Another user has updated this Organize while you were editing")
                    render(view: "edit", model: [organizeInstance: organizeInstance])
                    return
                }
            }
            organizeInstance.properties = params
            if (!organizeInstance.hasErrors() && organizeInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'organize.label', default: 'Organize'), organizeInstance])}"
                redirect(action: "list", id : organizeInstance?.parent?.id )
            }
            else {
                render(view: "edit", model: [organizeInstance: organizeInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'organize.label', default: 'Organize'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def organizeInstance = Organize.get(params.id)
        if (organizeInstance) {
            if (organizeInstance?.children) {
                log.info "categorys.delete children is extis ${categorysInstance?.children}"
                flash.message = "包含下属部门，所以不能删除。"
                return redirect(action: "list" ,id: categorysInstance?.parent?.id)
            }
            try {
                organizeInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'organize.label', default: 'Organize'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'organize.label', default: 'Organize'), params.id])}"
                redirect(action: "list", id: categorysInstance?.parent?.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'organize.label', default: 'Organize'), params.id])}"
            redirect(action: "list" ,id: params.id)
        }
    }
}
